package com.lecture.utils;

import com.lecture.pojo.FileInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ViedioFileUtil {

//    public static void main(String[] args) {
//        boolean boolen = processViedio("E:\\上课视频资料\\119\\day06-录屏", "E:\\上课视频资料\\119\\tmp.xls");
//        System.out.println(boolen);
//    }

    private static final List<String> suffixes =
            Collections.unmodifiableList(Arrays.asList("wmv", "mp4", "avi", "mod", "mkv", "itheima", "flv", "rm", "rmvb", "mpg"));

    /**
     * 批量获取图片信息
     * @param filePath
     * @param exportPath
     * @return
     */
    public static int processViedio(String filePath,String exportPath)  {
        File file = new File(filePath);
        ArrayList<FileInfo> infos = new ArrayList<>();
        if(file.isDirectory()){
            File[] files = file.listFiles();
            Arrays.asList(files).forEach(fi->{
                String suffix = fi.getName().substring(fi.getName().lastIndexOf(".") + 1);
                if(suffixes.contains(suffix)){
                    FileInfo info = getVidioInfo(fi);
                    infos.add(info);
                }
            });
        }else if (file.isFile()){
            String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            if(suffixes.contains(suffix)){
                FileInfo info = getVidioInfo(file);
                infos.add(info);
            }
        }
        //将集合排序
        Collections.sort(infos, new Comparator<FileInfo>() {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            @Override
            public int compare(FileInfo o1, FileInfo o2) {
                try {
                    return f.parse(o1.getCreateTime()).compareTo(f.parse(o2.getCreateTime()));
                } catch (ParseException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });
        return exportExcel(exportPath, infos);
    }

    /**
     * 导出excel
     * @param exportPath
     * @param infos
     * @return
     */
    private  static int exportExcel(String exportPath,List<FileInfo> infos){
        int num=0;
        XSSFWorkbook book = new XSSFWorkbook();
        //设置边框

        CellStyle cellStyle = book.createCellStyle(); //创建一个样式
        cellStyle.setBorderBottom(BorderStyle.DOUBLE); //底部边框
        //cellStyle.setBottomBorderColor(HSSFColor.YELLOW.index);//底部边框颜色
        cellStyle.setBorderLeft(BorderStyle.MEDIUM_DASH_DOT_DOT); //左边框
        //cellStyle.setLeftBorderColor(HSSFColor.RED.index);//左边框颜色
        cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); //设置背景色
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setBorderLeft(BorderStyle.MEDIUM_DASH_DOT_DOT); //左边

        Sheet sheet = book.createSheet();
        Row row0 = sheet.createRow(0);
        Cell cell0 = row0.createCell(0);
        cell0.setCellStyle(cellStyle);
        cell0.setCellValue("视频名称");
        Cell cell1 = row0.createCell(1);
        cell1.setCellStyle(cellStyle);
        cell1.setCellValue("时长");
        for (int i = 0; i < infos.size(); i++) {
            Row row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(infos.get(i).getName());
            row.createCell(1).setCellValue(infos.get(i).getDuration());
            //row.createCell(2).setCellValue(infos.get(i).getCreateTime());
        }
        try {
            FileOutputStream outputStream = new FileOutputStream(exportPath);
            book.write(outputStream);
            num=infos.size();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return num;
    }


    /**
     * 获取视频文件信息
     * @param file
     * @return
     */
    private static FileInfo getVidioInfo(File file)  {
        MultimediaObject instance = new MultimediaObject(file);
        MultimediaInfo info = null;
        try {
            info = instance.getInfo();
        } catch (EncoderException e) {
            e.printStackTrace();
        }

        int mi=(int)Math.ceil((double) info.getDuration() / 1000);
        String time=new StringBuilder().append(mi/60).append(":").append(mi%60).toString();
        String fileName = file.getName().substring(0,file.getName().lastIndexOf("."));
        FileInfo fileInfo = new FileInfo(fileName,time,getFileCreationTime(file));
        return fileInfo;
    }

    /**
     * 获取文件创建时间
     * @param file
     * @return
     */
    private static String getFileCreationTime(File file) {
        if (file == null) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        FileTime t= null;
        try {
            t = Files.readAttributes(Paths.get(file.getAbsolutePath()), BasicFileAttributes.class).creationTime();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String format = dateFormat.format(t.toMillis());
        return format;
    }







}
